<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class Contact extends Model
{
    //软删除
    use SoftDelete;

    //门店联系添加
    public function add($data)
    {
        //创建验证器
        $validate = new \app\common\validate\Contact();
        if (!$validate->scene('add')->check($data)){
            return $validate->getError();
        }
        //只验证数据库里有的字段并赋值给result
        $result = $this->allowField(true)->save($data);
        if ($result){
            return 1;
        }else{
            return '添加失败';
        }
    }

    //门店联系编辑
    public function edit($data)
    {
        //创建验证器
        $validate = new \app\common\validate\Contact();
        if (!$validate->check($data)){
            return $validate->getError();
        }
        //查询标题id赋值给$contactInfo
        $contactInfo = $this->find($data['id']);
        //更新
        $contactInfo->address = $data['address'];
        $contactInfo->mailbox = $data['mailbox'];
        $contactInfo->tel = $data['tel'];
        $contactInfo->trans = $data['trans'];
        //保存
        $result = $contactInfo->save();
        if ($result){
            return 1;
        }else{
            return '更新失败';
        }
    }
}
